*===============================================================================
* Do-file creates "Table A0: R2 from Predicting Residual Violence"
*===============================================================================
clear 
set more off

cd "~/Dropbox/VFD/REStat_Native_Files/Stata_Files"
local output "~/Dropbox/VFD/REStat_Native_Files/Stata_Files/Output"



use "monthpanel_analysis.dta", clear

keep if salary==1
gen trimmedobs=0
sum balance, d
replace trimmedobs =1 if (balance <=r(p99) & balance>=r(p1))
replace trimmedobs =. if balance ==.
bys identityid: egen trimmed_extusers = min(trimmedobs)
keep if trimmed_extusers==1


* Predicting the residuals
foreach y in violence { 
	reghdfe d_`y'_10km, absorb(ym identityid c.ym#district) cluster(district) residuals(r_`y')	
}

foreach var in d_violence_10km balance numtxns {
	forv i=1(1)4 {
		gen l`i'_`var'=l`i'.`var'		
	}
} 

foreach var in d_violence_10km balance numtxns {
	gen D1_`var'=D.`var'
	gen l1D1_`var'=l1.D1_`var'
	gen l2D1_`var'=l2.D1_`var'
	gen l3D1_`var'=l3.D1_`var'
}


* Columns 1-3
* Panel A, columns 1-3
estimates clear
	reg r_violence l(1).d_violence_10km
	est store violence_1_1
	estadd local lags "1"
	estadd local lmpaisa "-"
	
	reg r_violence l(1 2).d_violence_10km
	est store violence_1_2
	estadd local lags "2"
	estadd local lmpaisa "-"
	
	reg r_violence l(1 2 3).d_violence_10km
	est store violence_1_3
	estadd local lags "3"
	estadd local lmpaisa "-"
	
estout violence* ///
using "`output'/table_a0a.tex", ///
style(tex) mlabels(, none) collabels(, none) drop(*) ///
stats(r2 N lags lmpaisa, fmt(3 0 0 0) ///
label("R-Squared" "\# Observations" "Lags in violence" "Lags in M-Paisa")) ///
label title("Predicting Residual Violence") replace


* Panel B, columns 1-3 
estimates clear
	reg r_violence l(1).d_violence_10km l(1).balance l(1).numtxns
	est store violence_2_1
	estadd local lags "1"
	estadd local lmpaisa "1"

	reg r_violence l(1 2).d_violence_10km l(1 2).balance l(1 2).numtxns
	est store violence_2_2
	estadd local lags "2"
	estadd local lmpaisa "2"

	reg r_violence l(1 2 3).d_violence_10km l(1 2 3).balance l(1 2 3).numtxns
	est store violence_2_3
	estadd local lags "3"
	estadd local lmpaisa "3"

estout violence* ///
using "`output'/table_a0b.tex", ///
style(tex) mlabels(, none) collabels(, none) drop(*) ///
stats(r2 N lags lmpaisa, fmt(3 0 0 0) ///
label("R-Squared" "\# Observations" "Lags in violence" "Lags in M-Paisa")) ///
label title("Predicting Residual Violence") replace


* Panel C, columns 1-3
estimates clear
	reg r_violence l1D1_d_violence_10km
	est store violence_3_1
	estadd local trends "1"
	estadd local tmpaisa "-"

	reg r_violence l1D1_d_violence_10km l2D1_d_violence_10km
	est store violence_3_2
	estadd local trends "2"
	estadd local tmpaisa "-"

	reg r_violence l1D1_d_violence_10km l2D1_d_violence_10km l3D1_d_violence_10km
	est store violence_3_3
	estadd local trends "3"
	estadd local tmpaisa "-"

estout violence* ///
using "`output'/table_a0c.tex", ///
style(tex) mlabels(, none) collabels(, none) drop(*) ///
stats(r2 N lags lmpaisa, fmt(3 0 0 0) ///
label("R-Squared" "\# Observations" "Trends in viol." "Trends in M-Paisa")) ///
label title("Predicting Residual Violence") replace


* Panel D, columns 1-3
estimates clear
	reg r_violence l1D1_d_violence_10km l1D1_balance l1D1_numtxns
	est store violence_4_1
	estadd local trends "1"
	estadd local tmpaisa "1"

	reg r_violence l1D1_d_violence_10km l2D1_d_violence_10km l1D1_balance l1D1_numtxns l2D1_balance l2D1_numtxns
	est store violence_4_2
	estadd local trends "2"
	estadd local tmpaisa "2"

	reg r_violence l1D1_d_violence_10km l2D1_d_violence_10km l3D1_d_violence_10km l1D1_balance l1D1_numtxns l2D1_balance l2D1_numtxns l3D1_balance l3D1_numtxns
	est store violence_4_3
	estadd local trends "3"
	estadd local tmpaisa "3"

estout violence* ///
using "`output'/table_a0d.tex", ///
style(tex) mlabels(, none) collabels(, none) drop(*) ///
stats(r2 N trends tmpaisa, fmt(3 0 0 0) ///
label("R-Squared" "\# Observations" "Trends in viol." "Trends in M-Paisa")) ///
label title("Predicting Residual Violence") replace




* Columns 4-6
* Panel A, columns 4-6
estimates clear
* Column 4
qui:crtrees r_violence l1_d_vio, reg(l1_d_vio) stop(10) vcv(10) ///
seed(12345) tree
	scalar r2=e(r2_cv)
	scalar list r2
	
* Column 5	
qui:crtrees r_violence l1_d_vio l2_d_vio, reg(l1_d_vio l2_d_vio) stop(10) vcv(10) ///
seed(12345) tree
	scalar r2=e(r2_cv)
	scalar list r2
	
* Column 6
qui:crtrees r_violence l1_d_vio l2_d_vio l3_d_vio, reg(l1_d_vio l2_d_vio l3_d_vio) stop(10) vcv(10) ///
seed(12345) tree
	scalar r2=e(r2_cv)
	scalar list r2

* Panel B, columns 4-6
* Column 4
qui:crtrees r_violence l1_d_vio, reg(l1_d_vio l1_bal) stop(10) vcv(10) ///
seed(12345) tree
	scalar r2=e(r2_cv)
	scalar list r2
	
* Column 5
qui:crtrees r_violence l1_d_vio l2_d_vio, reg(l1_d_vio l2_d_vio l1_bal l2_bal) stop(10) vcv(10) ///
seed(12345) tree
	scalar r2=e(r2_cv)
	scalar list r2
	
* Column 6
qui:crtrees r_violence l1_d_vio l2_d_vio l3_d_vio, reg(l1_d_vio l2_d_vio l3_d_vio l1_bal l2_bal l3_bal) stop(10) vcv(10) ///
seed(12345) tree
	scalar r2=e(r2_cv)
	scalar list r2


* Panel C, columns 4-6
* Column 4
qui:crtrees r_violence l1D1_d_vio, reg(l1D1_d_vio) stop(10) vcv(10) ///
seed(12345) tree
	scalar r2=e(r2_cv)
	scalar list r2
	
* Column 5
qui:crtrees r_violence l1D1_d_vio l2D1_d_vio, reg(l1D1_d_vio l2D1_d_vio) stop(10) vcv(10) ///
seed(12345) tree
	scalar r2=e(r2_cv)
	scalar list r2
	
* Column 6
qui:crtrees r_violence l1D1_d_vio l2D1_d_vio l3D1_d_vio, reg(l1D1_d_vio l2D1_d_vio l3D1_d_vio) stop(10) vcv(10) ///
seed(12345) tree
	scalar r2=e(r2_cv)
	scalar list r2


* Panel D, columns 4-6	
* Column 4
qui:crtrees r_violence l1D1_d_vio, reg(l1D1_d_vio l1D1_balance) stop(10) vcv(10) ///
seed(12345) tree
	scalar r2=e(r2_cv)
	scalar list r2
	
* Column 5
qui:crtrees r_violence l1D1_d_vio l2D1_d_vio, reg(l1D1_d_vio l2D1_d_vio l1D1_balance l2D1_balance) stop(10) vcv(10) ///
seed(12345) tree
	scalar r2=e(r2_cv)
	scalar list r2
	
* Column 6
qui:crtrees r_violence l1D1_d_vio l2D1_d_vio l3D1_d_vio, reg(l1D1_d_vio l2D1_d_vio l3D1_d_vio l1D1_balance l2D1_balance l3D1_balance) stop(10) vcv(10) ///
seed(12345) tree
	scalar r2=e(r2_cv)
	scalar list r2


